import 'package:flutter/material.dart';
import '../theme/app_theme.dart';
import '../services/app_state_provider.dart';


import 'moon_companion_screen.dart' as moon_screen;
import 'minimalist_mode_screen.dart';
import 'record_screen.dart';
import 'profile_screen.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({super.key});

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  int _currentIndex = 0;
  bool _isLoading = true;
  bool _hasInitialized = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (!_hasInitialized) {
      _hasInitialized = true;
      _initializeScreen();
    }
  }

  Future<void> _initializeScreen() async {
    try {
      
      final appState = AppStateProvider.of(context);
      if (!appState.isInitialized) {
        await appState.initializeApp();
      }
      
      setState(() {
        _isLoading = false;
      });
    } catch (e) {
      debugPrint('MainScreen初始化失败: $e');
      
      await Future.delayed(const Duration(milliseconds: 500));
      if (mounted) {
        _initializeScreen();
      }
    }
  }

  Future<void> toggleMode(bool isMinimalist) async {
    final appState = AppStateProvider.of(context);
    try {
      await appState.setMinimalistMode(isMinimalist);
      
      
      setState(() {});
    } catch (e) {
      debugPrint('切换模式失败: $e');
      
    }
  }

  List<Widget> get _pages {
    final appState = AppStateProvider.of(context);
    return [
      appState.isMinimalistMode ? const MinimalistModeScreen() : const moon_screen.MoonCompanionScreen(),
      const RecordScreen(),
      ProfileScreen(onModeToggle: toggleMode, currentMode: appState.isMinimalistMode),
    ];
  }

  @override
  Widget build(BuildContext context) {
    if (_isLoading) {
      return Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: CircularProgressIndicator(color: AppTheme.primaryColor),
        ),
      );
    }

    return Scaffold(
      body: SafeArea(
        child: _pages[_currentIndex],
      ),
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }

  Widget _buildBottomNavigationBar() {
    return Container(
      decoration: const BoxDecoration(
        border: Border(top: BorderSide(color: Color(0xFFF0F0F0), width: 1)),
      ),
      child: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        selectedItemColor: AppTheme.primaryColor,
        unselectedItemColor: AppTheme.textLight,
        showSelectedLabels: true,
        showUnselectedLabels: true,
        backgroundColor: Colors.white,
        elevation: 0,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home_outlined),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.edit_note),
            label: '记录',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我',
          ),
        ],
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}